package com.hhkj.admin.student.common.utils;

import com.hhkj.admin.student.domain.Student;
import com.hhkj.admin.student.common.cont.StudentConst;
import com.ruoyi.common.utils.SecurityUtils;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * StudentUtils
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/26 14:17
 */
public class StudentUtils
{

    /**
     * 批量创建学员的密码信息
     *
     * @param studentDoList 学员列表
     */
    public synchronized static void batchEncryptPassword(List<Student> studentDoList)
    {
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        CountDownLatch countDownLatch = new CountDownLatch(studentDoList.size());
        for (Student studentDo : studentDoList) {
            executorService.execute(() -> {
                encryptPassword(studentDo);
                countDownLatch.countDown();
            });
        }

        try {
            // 等待一秒后检查子线程运行状态，全部完成返回true
            countDownLatch.await();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }

    /**
     * 设置学员的密码
     * @param studentReq 学员信息
     */
    public static void encryptPassword(Student studentReq)
    {
        String idCard = studentReq.getIdCard();
        String password = idCard.substring(idCard.length() - StudentConst.PASSWORD_NUM);
        studentReq.setPassword(SecurityUtils.encryptPassword(password));
    }
}
